In [17]:
%matplotlib inline
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import plotly as py
py.offline.init_notebook_mode()
import pandas as pd
import colorlover as cl
from IPython.display import HTML, display
import vincent
vincent.core.initialize_notebook()
In [2]:
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')
In [3]:
def get_string(x):
return "<br>".join("%s: %s" % (k.title(), x[k]) for k in [
"state", "beef", "total fruits",
"total veggies", "wheat", "corn"
])
In [4]:
df.head().apply(get_string, axis=1)
Out[4]:
In [5]:
for col in df.columns:
df[col] = df[col].astype(str)
scl = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],\
[0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]
df["text"] = df.apply(get_string, axis=1)
In [6]:
df.head()
Out[6]:
In [7]:
data = [ dict(
type='choropleth',
colorscale = scl,
autocolorscale = False,
locations = df['code'],
z = df['total exports'].astype(float),
locationmode = 'USA-states',
text = df['text'],
marker = dict(
line = dict (
color = 'rgb(255,255,255)',
width = 2
) ),
colorbar = dict(
title = "Millions USD")
) ]
layout = dict(
title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
geo = dict(
scope='usa',
projection=dict( type='albers usa' ),
showlakes = True,
lakecolor = 'rgb(255, 255, 255)'),
)
In [8]:
fig = dict( data=data, layout=layout )
py.offline.iplot( fig, filename='d3-cloropleth-map' )
In [10]:
df.head()
Out[10]:
In [20]:
world_topo = r'world-countries.topo.json'
geo_data = [{'name': 'countries',
'url': world_topo,
'feature': 'world-countries'}]
vis = vincent.Map(geo_data=geo_data, scale=200)
display(vis)
In [ ]: